import requests
import re
from setting import DB_POOLS
import redis

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0"
}

url = DB_POOLS
response = requests.get(url, headers=headers, timeout=5)
pattern = re.compile(r'"ip":"(.*?)","port":(\d+)')
matches = pattern.findall(response.text)

# 连接 Redis
r = redis.Redis(host='127.0.0.1', port=6379, decode_responses=True, encoding='utf-8')

for ip, port in matches:
    proxy = f'{ip}:{port}'
    # 检测代理是否可用
    try:
        test_url = 'http://httpbin.org/ip'
        response = requests.get(test_url, proxies={'http': proxy, 'https': proxy}, timeout=5)
        if response.status_code == 200:
            print(f'代理 {proxy} 有效，存入 Redis')
            # 存入 Redis
            r.sadd('proxy_pool', proxy)
        else:
            print(f'代理 {proxy} 无效')
    except requests.exceptions.RequestException:
        print(f'代理 {proxy} 无效')